var mysqlDB = require('../models/mysqlDb');
var logger = require('../models/logger');

var mysqlDBHelper = {};

mysqlDBHelper.streamingQueryRows = function(sql, processRow, datas) {
	logger.debug('sql',sql);
	var query = global.dbClient.query(sql);
	query
		.on('error', function(err) {
			// Handle error, an 'end' event will be emitted after this as well 
		})
		.on('fields', function(fields) {
			// the field packets for the rows to follow 
		})
		.on('result', function(row) {
			// Pausing the connnection is useful if your processing involves I/O 
			global.dbClient.pause();
			datas ? processRow(row, datas) : processRow(row);
		})
		.on('end', function() {
			logger.debug('all rows have been received');
		});
}

mysqlDBHelper.query = function(sql, cb) {
	logger.debug('sql',sql);
	global.dbClient.query(sql, function(err, results, fields) {
		logger.debug('err',err);
        if(err) {
        	logger.debug('err',err);
        }
        cb && cb(results);
    });
}

module.exports = mysqlDBHelper;